/**
 * 功能描述
 */

define(function (require, exports, module) {
	var aw = require("ajaxwrapper");
	
    module.exports = {
    		grid:{
    			 handleShowClick: function (widget, data) {
    				 var form = widget.get("form");
    				 widget.show([".J-form"]).hide([".J-grid"]);
					 widget.get("subnav").show(["return"]).hide(["dataSource","search"]);
					 form.setValue("name",data.personalInfo.name);
					 form.setValue("sex",data.personalInfo.sex.key);
					 form.setValue("idNumber",data.personalInfo.idNumber);
					 form.setValue("mobilePhone",data.personalInfo.mobilePhone);
					 form.setValue("birthday",data.personalInfo.birthday);
					 var year = parseInt(moment().format("YYYY-MM-DD").substr(0,4));
					 var value = (form.getValue("birthday")==null||form.getValue("birthday")=="")?year:form.getValue("birthday");
					 var birthday_year = parseInt(moment(value).format("YYYY-MM-DD").substr(0,4));
					// form.setValue("age",year-birthday_year==null?0:year-birthday_year);
					 form.setValue("age",(form.getValue("birthday")==null||form.getValue("birthday")=="")?0:year-birthday_year);
					 form.setValue("wechat",data.wechat);
					 form.setValue("qq",data.qq);
					 form.setValue("electronicMail",data.personalInfo.electronicMail);
					 form.setValue("houseRegister",data.personalInfo.houseRegister==null?null:data.personalInfo.houseRegister.fullName);
					 form.setValue("zipCode",data.zipCode);
					 form.setValue("address",data.personalInfo.address);
					 form.setValue("dataSource",data.dataSource.key=="Visit"?"意向客户":"体验客户");
					 form.setValue("product",data.productType==null?null:data.productType.product.name);
					 form.setValue("productType",data.productType==null?null:data.productType.name);
					 form.setValue("intention",data.intention==null?null:data.intention.value);
					 form.setValue("visitTime",data.visitTime);
					 form.setValue("saleConsultant",data.saleConsultant.name);
					 form.setDisabled(true);
					 aw.ajax({
	        				url : "api/customervisitrecord/query",
	        				data : {
	        					"customer.pkCustomer":data.pkCustomer,
	        					"recordType":"Visit",
	            				fetchProperties:"*,customer.*," +
	            						"customer.organization.*," +
			            				"customer.personalInfo.*," +
			            				"customer.personalInfo.houseRegister.*," +
			            				"customer.productType.*," +
			            				"customer.saleConsultant.*," +
			            				"customer.intention.*," +
			            				"customer.status.*," +
			            				"customer.dataSource.*," +
			            				"visitWay.*"
	        				},
	        				dataType : "json",
	        				success : function(datas){
	        					var visit = {};
	        					for(var i in datas){
	        						if(data.visitTime == datas[i].visitDate){
	        							visit=datas[i];
	        							break;
	        						}
	        					}
	        					 form.setData("visitWay",[{pkVisitWay:visit.visitWay==null?null:visit.visitWay.pkVisitWay,name:visit.visitWay==null?null:visit.visitWay.name}])
//	        					 form.setValue("visitWay",visit.visitWay.pkVisitWay);
	        					 form.setValue("contents",visit.contents);
	        					 form.setValue("description",visit.description);
	        					 form.setValue("visitWay",visit.visitWay==null?null:visit.visitWay.pkVisitWay);
	        				}
	        		});
    			 }
    		}
    };
});
